#include "importdialog.h"
#include <QVBoxLayout>
#include <QFormLayout>
#include <QLineEdit>
#include <QComboBox>
#include <QCheckBox>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QLabel>
#include <QFileDialog>
#include <QSqlDatabase>
#include <QApplication>

ImportDialog::ImportDialog(const QSqlDatabase &db, QWidget *parent)
    : QDialog(parent)
    , m_db(db)
{
    setupUI();
    setupConnections();
    refreshTables();
    validateInput();
}

QString ImportDialog::getFileName() const
{
    return m_fileEdit->text();
}

QString ImportDialog::getTableName() const
{
    return m_tableCombo->currentText();
}

int ImportDialog::getFormat() const
{
    return m_formatCombo->currentData().toInt();
}

bool ImportDialog::getFirstRowHeader() const
{
    return m_firstRowHeaderCheck->isChecked();
}

void ImportDialog::browseFile()
{
    QString filter = tr("All Supported Files (*.csv *.json *.xml *.sql);;"
                        "CSV Files (*.csv);;"
                        "JSON Files (*.json);;"
                        "XML Files (*.xml);;"
                        "SQL Files (*.sql);;"
                        "All Files (*)");
    
    QString fileName = QFileDialog::getOpenFileName(this, tr("Select Import File"), 
                                                   QString(), filter);
    if (!fileName.isEmpty()) {
        m_fileEdit->setText(fileName);
        
        // 根据文件扩展名自动设置格式
        if (fileName.endsWith(".csv", Qt::CaseInsensitive)) {
            m_formatCombo->setCurrentIndex(0); // CSV
        } else if (fileName.endsWith(".json", Qt::CaseInsensitive)) {
            m_formatCombo->setCurrentIndex(1); // JSON
        } else if (fileName.endsWith(".xml", Qt::CaseInsensitive)) {
            m_formatCombo->setCurrentIndex(2); // XML
        } else if (fileName.endsWith(".sql", Qt::CaseInsensitive)) {
            m_formatCombo->setCurrentIndex(3); // SQL
        }
    }
}

void ImportDialog::validateInput()
{
    bool valid = !m_fileEdit->text().isEmpty() && 
                 !m_tableCombo->currentText().isEmpty();
    
    m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid);
}

void ImportDialog::refreshTables()
{
    m_tableCombo->clear();
    if (m_db.isOpen()) {
        m_tableCombo->addItems(m_db.tables());
    }
}

void ImportDialog::setupUI()
{
    setWindowTitle(tr("Import Data"));
    setModal(true);
    
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    
    QFormLayout *formLayout = new QFormLayout;
    
    // 文件选择
    QHBoxLayout *fileLayout = new QHBoxLayout;
    m_fileEdit = new QLineEdit;
    m_browseButton = new QPushButton(tr("Browse..."));
    fileLayout->addWidget(m_fileEdit);
    fileLayout->addWidget(m_browseButton);
    formLayout->addRow(tr("File:"), fileLayout);
    
    // 格式选择
    m_formatCombo = new QComboBox;
    m_formatCombo->addItem(tr("CSV"), 0);
    m_formatCombo->addItem(tr("JSON"), 1);
    m_formatCombo->addItem(tr("XML"), 2);
    m_formatCombo->addItem(tr("SQL"), 3);
    formLayout->addRow(tr("Format:"), m_formatCombo);
    
    // 表选择
    m_tableCombo = new QComboBox;
    m_tableCombo->setEditable(true);
    formLayout->addRow(tr("Table:"), m_tableCombo);
    
    // CSV选项
    m_firstRowHeaderCheck = new QCheckBox(tr("First row as header"));
    m_firstRowHeaderCheck->setChecked(true);
    formLayout->addRow("", m_firstRowHeaderCheck);
    
    mainLayout->addLayout(formLayout);
    
    m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
    mainLayout->addWidget(m_buttonBox);
    
    connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
}

void ImportDialog::setupConnections()
{
    connect(m_browseButton, &QPushButton::clicked, this, &ImportDialog::browseFile);
    connect(m_fileEdit, &QLineEdit::textChanged, this, &ImportDialog::validateInput);
    connect(m_tableCombo, &QComboBox::currentTextChanged, this, &ImportDialog::validateInput);
}